package cn.javabb.admin.controller.biz;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.common.model.Tree;
import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.biz.CatalogDO;
import cn.javabb.core.service.biz.CatalogService;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2018/11/27/027 15:19
 * @Since V1.0
 */
@Controller
@RequestMapping("/admin/catalog")
public class CatalogAdminController extends BaseController {
    @Autowired
    private CatalogService catalogService;
    /**
     * 菜单列表页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String toListPage(Model model) {
        return "admin/biz/catalog/catalogList";
    }
    /**
     * 菜单保存页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/save")
    public String toSavePage(Model model) {

        return "admin/biz/catalog/catalogSave";
    }
    @GetMapping("/type")
    public String toTypePage(Model model) {

        return "admin/biz/catalog/catalogTypeManage";
    }
    @ResponseBody
    @GetMapping("/tree")
    public Object tree(){
        List<CatalogDO> cList = catalogService.treeTable();
        return ResponseModel.Success(cList);
    }
    @ResponseBody
    @GetMapping("/treeSelect")
    public Object treeSelect(){
        List<Tree> tList = catalogService.listTree();

        return ResponseModel.Success(tList,(Integer)200);
    }
    /**
     * 查询列表
     */
    @ResponseBody
    @GetMapping("/")
    public Object list() {
        String page = getPage();
        String size = getSize();
        String catalogName = getParamValue("catalogName");

        System.out.println(catalogName);
        CatalogDO catalog = new CatalogDO();
        if(StringUtils.isNotBlank(catalogName)){
            catalog.setCatalogName(catalogName);
        }
        Page catalogPage = catalogService.queryPageByWhere(page, size, catalog);
        return ResponseModel.table(catalogPage);
    }

    /**
     * 更新
     * @param catalog
     * @return
     */
    @ResponseBody
    @PostMapping("/")
    public Object saveOrUpdate(CatalogDO catalog){
        int n = 0;
        if(null == catalog.getCatalogId()){
            if(null == catalog.getState()){catalog.setState(1);}
            if (null == catalog.getParentId()) {
                catalog.setParentId(0);
            }
            n = catalogService.insertSelective(catalog);
        }else{
            n = catalogService.updateSelective(catalog);
        }
        if(n>0){
            return ResponseModel.Success("保存成功");
        }
        return ResponseModel.Failure("保存失败");

    }

    /**
     * 删除
     * @param catalogId
     * @return
     */
    @ResponseBody
    @DeleteMapping("/{catalogId}")
    public Object delete(@PathVariable String catalogId){
        if(StringUtils.isBlank(catalogId)){
            return ResponseModel.Failure("参数错误");
        }
        List<String> idList = ListUtils.stringToListString(catalogId);
        int num = catalogService.deleteByIds(CatalogDO.class, "catalogId", idList);
        if(num>0){
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }

    /**
     * 更新状态
     * @param catalogId
     * @param state
     * @return
     */
    @ResponseBody
    @PostMapping("/updateState")
    public Object updateState(Integer catalogId,Integer state){
        CatalogDO c = new CatalogDO();
        c.setState(state);
        c.setCatalogId(catalogId);
        if(catalogService.updateSelective(c)>0){
            return ResponseModel.Success("状态更新成功");
        }
        return ResponseModel.Failure("状态更新失败");
    }

}
